package com.xuxiangyang.competitionsystem.controller;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.NumbericRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.policy.ListRenderPolicy;
import com.xuxiangyang.competitionsystem.entities.Group;
import com.xuxiangyang.competitionsystem.entities.RetResponse;
import com.xuxiangyang.competitionsystem.entities.RetResult;
import com.xuxiangyang.competitionsystem.entities.TemplateDate;
import com.xuxiangyang.competitionsystem.service.CompService;
import com.xuxiangyang.competitionsystem.service.ICompService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/upload")
@CrossOrigin
public class uploadController {

    @Autowired
    private com.xuxiangyang.competitionsystem.tools.FileUtils fileUtils;

    @Autowired
    private ICompService compService;

    //上传模板至临时文件夹
    @RequestMapping("/template")
    public RetResult upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        String fileUrl;
//        启动存储
        String path = ResourceUtils.getURL("classpath:").getPath()+"static/temporary/";
//        永久存储
        String pathLong = "/src/main/resources/static/temporary/";
        String realPath = path.replace('/', '\\').substring(1,path.length());
        String realPathLong = pathLong.replace('/', '\\').substring(1,pathLong.length());
        String[] split = file.getOriginalFilename().split("\\.");
        String suffix = split[split.length-1];
        String fileName = System.currentTimeMillis() + "." + suffix;
        System.out.println("realPath:" + realPath);
        System.out.println("fileName:" + fileName);
        //方法返回的图片url 如："http://127.0.0.1:8080/upload/imgs/logo.jpg.png"
        String url = "";
        if (Objects.isNull(file) || file.isEmpty()) {
            System.out.println("文件为空");
        }
        try {
            //写入目的地
            FileUtils.writeByteArrayToFile(new File(realPath + fileName),
                    file.getBytes());
            FileUtils.writeByteArrayToFile(new File(realPathLong + fileName),
                    file.getBytes());
        } catch (Exception e) {
            System.out.println("error");
        }
        System.out.println(url + fileName);
//        fileUrl = url + fileName;
        fileUrl = fileUtils.getUrl(request,"static/temporary/",fileName);
        return RetResponse.makeOKRsp(fileUrl);
    }

//    智能排版
    @RequestMapping("/typeSetting")
    public RetResult typeSetting(HttpServletRequest request, TemplateDate templateDate) throws IOException {
        String[] split = templateDate.getTempUrl().split("/");
        String templiateName = split[split.length - 1];
        String[] splitFileName = templiateName.split("\\.");
        String suffix = splitFileName[splitFileName.length-1];
        String outName = templateDate.getGroupId() + "." + suffix;
        fileUtils.typesetting(templateDate, templiateName, outName,"src/main/resources/static/out/");
        fileUtils.typesetting(templateDate, templiateName, outName,ResourceUtils.getURL("classpath:").getPath() +"static/out/");
        String fileUrl = fileUtils.getUrl(request,"static/out/",outName);
        System.out.println(fileUrl);
        Group group = new Group();
        group.setGroupId(templateDate.getGroupId());
        group.setSrc(fileUrl);
        compService.prospectus(group);
        return RetResponse.makeOKRsp(fileUrl);
    }

    //    重新上传商业计划书
    @RequestMapping("/reSubmit")
    public RetResult reSubmit(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        //        启动存储
        String path = ResourceUtils.getURL("classpath:").getPath()+"static/out/";
//        永久存储
        String pathLong = "/src/main/resources/static/out/";
        String realPath = path.replace('/', '\\').substring(1,path.length());
        String realPathLong = pathLong.replace('/', '\\').substring(1,pathLong.length());
        String fileUrl = fileUtils.storage(request, file, realPath, fileName);
        fileUtils.storage(request, file, realPathLong, fileName);
        System.out.println(fileUrl);
        return RetResponse.makeOKRsp();
    }
}
